package com.arch.dev.client.view.chart;

import java.util.Arrays;

import java.util.Date;

import java.util.List;

import com.arch.dev.client.vo.BudgetVO;
import com.arch.dev.client.vo.MonthVO;
import com.google.gwt.user.client.ui.Button;

import com.googlecode.gchart.client.GChart;

/**
 * 
 * Defines a traditional "quarterly revenues" grouped bar-chart.
 */

public class ExpenditureChart extends GChart {

	final String[] groupLabels = { "<html>2007<br><small><small>O Seven",

	"<html>2008<br><small><small>Owe Ate",

	"<html>2009<br><i><small><small>Oh Nein!" };

	final String[] barLabels = { "Q1", "Q2", "Q3", "Q4" };

	final String[] barColors = { "red", "blue", "green", "silver" };

	final int MAX_REVENUE = 1000;

	final int WIDTH = 300;

	final int HEIGHT = 200;

	Button updateButton = new Button(

	"<b><big>Generate New Simulated Revenues</big></b>");

	ExpChartType expChartType;

	public ExpenditureChart(ExpChartType expChartType) {
		this();
		this.expChartType = expChartType;
		if (ExpChartType.DAILY == expChartType) {
			setChartTitle("<b>Daily Expenditure</b>");
			getXAxis().setAxisLabel("<b>Date</b>");
			getCurve().getSymbol().setBackgroundColor("red");
		} else if (ExpChartType.MONTHLY == expChartType) {
			setChartTitle("<b>Monthly Expenditure</b>");
			getXAxis().setAxisLabel("<b>Month</b>");
			getCurve().getSymbol().setBackgroundColor("green");
		}

	}

	public ExpenditureChart() {

		setChartSize(800, 200);

		addCurve();

		// setData(new int[]{200,200,900,300,600}, new

		// String[]{"1","2","3","4","5"});

		getCurve().setLegendLabel("Cost");

		getCurve().getSymbol().setSymbolType(SymbolType.VBAR_SOUTH);

		getCurve().getSymbol().setBorderColor("black");

		// getCurve().getSymbol().setModelWidth(1.0);

		getXAxis().setHasGridlines(false);

		getYAxis().setAxisLabel("<b>Cost</b>");

		getYAxis().setHasGridlines(false);
		getCurve().getSymbol().setWidth(30);

		getXAxis().setAxisMin(0); // keeps first bar on chart

		getYAxis().setTickCount(11);

		getYAxis().setAxisMin(0); // Based on sim revenue range

		getYAxis().setAxisMax(MAX_REVENUE); // of 0 to MAX_REVENUE.

	}

	public void processData(List<BudgetVO> budgetVOs) {

		double[] costValues = new double[budgetVOs.size()];

		String[] label = new String[budgetVOs.size()];

		int i = 0;

		for (BudgetVO budgetVO : budgetVOs) {

			costValues[i] = budgetVO.getCost().doubleValue();

			Date date = budgetVO.getDate();

			if (ExpChartType.DAILY == expChartType) {
				label[i] = getDateFromDate(date);
			} else if (ExpChartType.MONTHLY == expChartType) {
				label[i] = getMonthFromDate(date);
			}

			i++;

		}

		setData(costValues, label);

	}

	public void setData(double[] y, String[] xAxisLabel) {

		setChartSize(800, 300);
		getCurve().clearPoints();

		getXAxis().setTickCount(y.length * 2);

		getYAxis().setAxisMax(getMaxYAxisValue(y));

		int i = 1;

		for (int j = 0; j < y.length; j++) {

			getCurve().addPoint(i, y[j]);

			getXAxis().addTick(i, xAxisLabel[j]);

			getCurve().getPoint().setAnnotationText(Double.toString(y[j]));

			getCurve().getPoint().setAnnotationLocation(
					AnnotationLocation.NORTH);

			i = i + 2;

		}

		update();

	}

	private int getMaxYAxisValue(double[] y) {

		double[] yXis = new double[y.length];
		for (int i = 0; i < yXis.length; i++) {
			yXis[i] = y[i];
		}
		Arrays.sort(yXis);
		int value = ((int) yXis[yXis.length - 1]) + 100;

		if (value > MAX_REVENUE) {
			return value;
		} else {
			return MAX_REVENUE;
		}

	}

	private String getMonthFromDate(Date date) {

		MonthVO monthVO = new MonthVO();
		monthVO.setId(date.getMonth());

		return monthVO.getName();

	}

	private String getDateFromDate(Date date) {

		return Integer.toString(date.getDate());

	}

}